package com.fr.chicken.manager.account;

import com.fr.chicken.dao.AccountListDao;
import com.fr.chicken.facade.request.AccountInfo;
import com.fr.chicken.facade.request.UserRegisterRequsetDTO;
import com.fr.chicken.model.AccountList;
import com.fr.chicken.model.PlayerInfo;
import com.fr.chicken.util.Md5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountManager {

    @Autowired
    private AccountListDao accountListDao;

    public void createAccountList(AccountInfo accountInfo, long playerId) {
        AccountList accountList = new AccountList();
        accountList.setPlayerId(playerId);
        accountList.setPlatformType(accountInfo.getPlatformFlag());
        accountList.setAccount(accountInfo.getAccount());
        if (accountInfo.getPlatformFlag()==3){
            accountList.setPassword(Md5Util.getMD5(accountInfo.getPwd()).toLowerCase());
        }
        if (accountInfo.getPlatformFlag()==2){
            accountList.setPassword(accountInfo.getPwd());
        }
        int num2 = accountListDao.insertSelective(accountList);
        if (num2<1){
            throw new RuntimeException("插入用户信息失败");
        }
    }

    public void createAccountList(Long playerId,int platform,String account,String password) {
        AccountList accountList = new AccountList();
        accountList.setPlayerId(playerId);
        accountList.setPlatformType(platform);
        accountList.setAccount(account);
        accountList.setPassword(password);
        int num2 = accountListDao.insertSelective(accountList);
        if (num2<1){
            throw new RuntimeException("插入用户信息失败");
        }
    }

    public boolean checkAccountExistByPhoneNumber(String phongNumbaer){
        if(StringUtils.isBlank(phongNumbaer)){
            return false;
        }
      return  accountListDao.selectByAccount(phongNumbaer) == null;
    }

    /**
     * 根据手机号查找账号信息
     * */
    public AccountList findAccount(String account){
        return  accountListDao.selectByAccount(account);
    }

    /**
     * 查找玩家关联的账号
     * */
    public List<AccountList> findAccountByPlayerId(long playerId){
        return  accountListDao.selectByPlayerId(playerId);
    }

    /**
     * 查询玩家所有关联的平台
     * */
    public List<Integer> queryPlayerPlatforms(long playerId){
        return  accountListDao.selectPlatformsPyPlayerId(playerId);
    }

    /**
     * 玩家是否已经绑定某个平台
     * */
    public boolean checkPlayerBindedPlatforms(long playerId,int platform){
        List<Integer> playerPlatforms =  queryPlayerPlatforms(playerId);
        if(playerPlatforms != null && playerPlatforms.contains(platform)){
            return true;
        }
        return false;
    }

}
